#测试登录页面
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
from common.Utils import BlogDriver
from selenium.webdriver.common.alert import Alert


class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

        #self.driver.implicitly_wait(2)
    #成功登录测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        #找到登录成功后界面的一个元素，说明登陆成功
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        #BlogDriver.getScreeShot()
        #print("=== 关闭浏览器前的状态 ===")
        #print("最终URL:", self.driver.current_url)
        #print("最终标题:", self.driver.title)

        #self.driver.back()

    # 失败登录测试用例
    def LoginFailTest_01(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        # 输入错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        assert "密码错误" in alert_text
        alert.accept()

    def LoginFailTest_02(self):
        #密码正确，用户名输入错误
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        assert "用户不存在" in alert_text
        alert.accept()

    def LoginFailTest_03(self):
        #只有用户名，密码为空
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        assert "账号或密码不能为空" in alert_text
        alert.accept()

    def LoginFailTest_04(self):
        #用户名为空，密码正确
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        assert "账号或密码不能为空" in alert_text
        alert.accept()

    def LoginFailTest_05(self):
        #用户名错误，密码错误
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        assert "用户不存在" in alert_text
        alert.accept()



#login = BlogLogin()
#login.LoginSucTest()
#login.LoginFailTest_01()# 输入错误的密码
#login.LoginFailTest_02()#密码正确，用户名输入错误
#login.LoginFailTest_03()#只有用户名，密码为空
#login.LoginFailTest_04()#用户名为空，密码正确
#login.LoginFailTest_05()#用户名错误，密码错误




